"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 使用alter user方式修改autocommit为off，连接B模式数据库，查询为off
Create At   : 2023/01/16
@zou_jialiang0501162244
Description :
    1、创建兼容B模式数据库
    2、连接B库，创建用户并设置用户autocommit=0
    3、使用创建的用户连接B库，查询autocommit
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、查询结果为off
    4、成功
History     :
"""

import os
import unittest

from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_autocommit_0006'
        self.u_name = 'u_autocommit_0006'

    def test_autocommit(self):
        text1 = '-----step1:创建兼容B模式数据库;expect:成功-----'
        self.log.info(text1)
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility 'B';"
        createdb_res = self.pri_sh.execut_db_sql(create_db)
        self.log.info(createdb_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, createdb_res,
                      f"执行失败: {text1}")

        text2 = '-----step2:连接兼容B库，创建用户并设置用户autocommit=0;' \
                'expect:执行成功-----'
        self.log.info(text2)
        sql = f"drop user if exists {self.u_name} cascade;" \
              f"create user {self.u_name} password '{macro.COMMON_PASSWD}';" \
              f"alter user {self.u_name} set autocommit to off;"
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res,
                      f'创建用户失败: {text2}')
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, sql_res,
                      f'修改autocommit参数失败: {text2}')

        text3 = '-----step3:使用创建的用户连接B库，查询autocommit;' \
                'expect:查询结果为off-----'
        self.log.info(text3)
        show_res = self.pri_sh.execut_db_sql(
            'show autocommit;',
            sql_type=f'-U {self.u_name} -W{macro.COMMON_PASSWD}',
            dbname=self.db_name)
        self.log.info(show_res)
        self.assertEqual(show_res.splitlines()[-2].strip(), 'off',
                         f'执行失败: {text3}')

    def tearDown(self):
        text4 = '-----step4:清理环境; expect:成功-----'
        self.log.info(text4)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};"
            f"drop user if exists {self.u_name} cascade;")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      f'删库失败: {text4}')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_res,
                      f'删除用户失败: {text4}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
